package com.example.mainstream.ui;

import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import androidx.lifecycle.ViewModelProvider;

import com.example.shopapp.R;
import com.example.shopapp.dao.UserDao;
import com.example.shopapp.database.GreenDaoManager;
import com.example.shopapp.databinding.ActivityRegisterBinding;
import com.example.shopapp.model.IMerchantRegisterListener;
import com.example.shopapp.model.IUserRegisterListener;
import com.example.shopapp.model.RegisterViewModel;

import cn.project.base.baseui.BaseNormalActivity;

public class RegisterActivity extends BaseNormalActivity<ActivityRegisterBinding> {

    private RegisterViewModel registerViewModel; // 注册ViewModel对象
    private int standing = 0; // 身份 0 用户 1商家

    UserDao userDao;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register; // 返回布局文件的ID
    }

    @Override
    protected void initView() {
        registerViewModel = new ViewModelProvider(this).get(RegisterViewModel.class); // 初始化ViewModel对象
    }

    @Override
    protected void initData() {
        userDao = GreenDaoManager.getmInstance().getDaoSession().getUserDao();
        // 设置下拉框的选择监听器
        mBinding.spShenfen.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                standing = position; // 获取选择的身份
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        mBinding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = mBinding.inputName.getText().toString(); // 获取输入的用户名
                String password = mBinding.inputPassword.getText().toString(); // 获取输入的密码
                String passwordagegin = mBinding.inputPassword.getText().toString(); // 获取再次输入的密码

                // 判断用户名是否为空
                if (TextUtils.isEmpty(name)){
                    showToast("请输入用户名");
                    return;
                }

                // 判断密码是否为空
                if (TextUtils.isEmpty(password)){
                    showToast("请输入密码");
                    return;
                }
                if (TextUtils.isEmpty(passwordagegin)){
                    showToast("请输入密码");
                    return;
                }
                if (!passwordagegin.equals(password)){
                    showToast("密码不一致");
                    return;
                }
                if (standing == 0){
                    userRegister(name,password); // 用户注册
                    return;
                }
                merchantRegister(name,password); // 商家注册
            }
        });
    }

    private void merchantRegister(String name, String password) {
        registerViewModel.merchantRegister(name, password, new IMerchantRegisterListener() {
            @Override
            public void merchantRegisterSuccess() {
                showToast("商家注册成功");
                finish();
            }

            @Override
            public void merchantRegisterFail(String message) {
                showToast(message);
            }
        });
    }


    private void userRegister(String name,String password){
        registerViewModel.userRegister(name, password, new IUserRegisterListener() {
            @Override
            public void userRegisterSuccess() {
                showToast("用户注册成功");
                finish();
            }

            @Override
            public void userRegisterFail(String message) {
                showToast(message);
            }
        });
    }
}
